// pages/other//redshare/index.js
const Page = require('../../../utils/ald-stat.js').Page
const app = getApp()
const util = require('../../../utils/throttle.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canvasHeight: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  saveImageToPhotos: util.throttle(function () {
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              that.setData({
                writePhotosAlbum: true
              })
              that.sharefriend()
            },
            fail() {
              wx.showModal({
                title: '提示',
                content: '保存相册授权未开启，无法保存到手机，请重新授权！',
                success: function (re) {
                  if (re.confirm) {
                    wx.openSetting({
                      success: res => {
                        if (res.authSetting['scope.werun']) {
                          that.setData({
                            writePhotosAlbum: true
                          })
                        }
                      },
                      fail: function (data) {
                        that.setData({
                          writePhotosAlbum: false
                        })
                      }
                    })
                  }
                }
              })
            }
          })
        } else {
          that.sharefriend()
        }
      }
    })
  }, 1500),
  sharefriend: function () {
    const that = this
    wx.showLoading({
      title: '努力生成中...'
    })
    var url =
      'https://www.mnancheng.com/admin/wechat/get_qrcode?openid=' +
      wx.getStorageSync('openid')
    const windowWidth = wx.getSystemInfoSync()
    const backgroundPromise =
      'https://qizhifan.oss-cn-hangzhou.aliyuncs.com/zdd/system/applet/phonebg.jpg'
    wx.downloadFile({
      url: url,
      success(res) {
        const erweima = res.tempFilePath
        wx.getImageInfo({
          src: backgroundPromise,
          success(res) {
            var canvasWidth = windowWidth.screenWidth * 2
            var canvasHeight = windowWidth.screenHeight * 2
            that.setData({
              canvasHeight: canvasHeight
            })
            const ctx = wx.createCanvasContext('shareCanvas')
            // 底图
            ctx.drawImage(
              backgroundPromise,
              0,
              0,
              canvasWidth / 2,
              canvasHeight / 2
            )
            const qrImgSize = 160
            ctx.drawImage(
              erweima,
              canvasWidth / 4 - 80,
              canvasHeight / 4 - 108,
              qrImgSize,
              qrImgSize
            )
            // 播放按钮
            ctx.stroke()
            ctx.draw()
            setTimeout(function () {
              wx.canvasToTempFilePath({
                canvasId: 'shareCanvas',
                success: function (res) {
                  wx.hideLoading()
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath, // 此为图片路径
                    success: res => {
                      wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                        duration: 2000
                      })
                      return
                    },
                    fail: err => {
                      wx.showToast({
                        title: '保存失败',
                        icon: 'success',
                        duration: 2000
                      })
                    }
                  })
                }
              })
            }, 500)
          }
        })
      }
    })
  },
  onShareAppMessage: function (res) {
    var openid = wx.getStorageSync('openid')
    return {
      title: '每天走多多，现金红包轻松拿',
      imageUrl: '../../../imgs/openshare.jpg',
      path: '/pages/tarbar/index/index?openid=' + openid + '&&jx=55'
    }
  }
})
